package indi.zhifa.study2024.nbr.business.report.consign.entity.response;

import indi.zhifa.study2024.nbr.business.report.consign.entity.mapperOut.ConsignMapperOut;
import indi.zhifa.study2024.nbr.enums.item.EItemUnitType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class ConsignGroupByItemReportResponse extends BaseConsignReportResponse {

    @Schema(title = "商品Id")
    Long itemId;
    @Schema(title = "商品名")
    String itemName;
    EItemUnitType unitType;
    @Schema(title = "小单位名称")
    String smallUnitName;

    @Schema(title = "中单位名称")
    String midUnitName;

    @Schema(title = "大单位名称")
    String largeUnitName;

    @Schema(title = "一个大单位等于几个小单位")
    Integer largeConvert;

    @Schema(title = "一个中单位等于几个小单位")
    Integer midConvert;

    @Schema(title = "售出大单位数量")
    Long saleCountLargeUnit;

    @Schema(title = "售出中单位数量")
    Long saleCountMidUnit;

    @Schema(title = "售出小单位数量")
    Long saleCountSmallUnit;

    @Schema(title = "退货大单位数量")
    Long returnCountLargeUnit;

    @Schema(title = "退货中单位数量")
    Long returnCountMidUnit;

    @Schema(title = "退货小单位数量")
    Long returnCountSmallUnit;

    @Schema(title = "销售数量")
    Long saleCount;
    @Schema(title = "退货数量")
    Long returnCount;
    @Schema(title = "净销售数量")
    Long nestCount;


    @Schema(title = "退货率")
    Double returnRate;


    @Override
    protected void onInitWithMapperOut(ConsignMapperOut pConsignMapperOut) {
        itemId = pConsignMapperOut.getItemId();
        itemName = pConsignMapperOut.getItemName();
        unitType = pConsignMapperOut.getUnitType();
        smallUnitName = pConsignMapperOut.getSmallUnitName();
        midUnitName = pConsignMapperOut.getMidUnitName();
        largeUnitName = pConsignMapperOut.getLargeUnitName();
        largeConvert = pConsignMapperOut.getLargeConvert();
        midConvert = pConsignMapperOut.getMidConvert();

        saleCount = pConsignMapperOut.getSaleCnt();
        returnCount = pConsignMapperOut.getReturnCnt();
        nestCount = pConsignMapperOut.getNestCnt();

        if(saleCount > 0){
            returnRate = BigDecimal.valueOf(returnCount).divide(BigDecimal.valueOf(saleCount), 4, BigDecimal.ROUND_HALF_UP).doubleValue();
        }else{
            returnRate = 0.0;
        }

    }
}
